import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class proses_Login extends JWindow {
  JLabel jLabel1 = new JLabel();
  JProgressBar prosesBar = new JProgressBar();
  JLabel statusProses = new JLabel();
  Connection konek;
  javax.swing.Timer time;
  ActionListener task1, task2, task3, task4, task5, task6;
  String dataUser, dataPassword;
  boolean proses = true;
  String statusGagal;


  public proses_Login(String user, String password) {
    dataUser = user;
    dataPassword = password;
    tampilan_GUI();
    tahapan_prosesLogin();
  }

  public void tampilan_GUI() {
    this.getContentPane().setBackground(Color.yellow);
    this.getContentPane().setLayout(null);
    jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
    jLabel1.setText("Tunggu Sebentar ...");
    jLabel1.setBounds(new Rectangle(14, 5, 187, 30));
    prosesBar.setBounds(new Rectangle(14, 40, 311, 16));
    prosesBar.setStringPainted(true);
    statusProses.setText("");
    statusProses.setBounds(new Rectangle(14, 60, 311, 16));
    this.getContentPane().add(jLabel1);
    this.getContentPane().add(prosesBar);
    this.getContentPane().add(statusProses);

  }


  public void load_DriverJDBC() {
    // Mencoba memanggil Driver JDBC
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      status_Proses(true, "Sukses!!! Driver JDBC ditemukan ...", 20);
      // Gagal Memanggil Driver JDBC
    }
    catch (ClassNotFoundException cnfe) {
      status_Proses(false, "Gagal!!! Driver JDBC tidak ditemukan ...", 20);
    }
  }

  /**
   * Methode koneksiDatabase
   * Untuk melakukan koneksi dengan Database Server
   * Database Server = MySQL
   * getConection yang digunakan adalah getConnection(url, properties)
   */
  public void koneksiDatabase() {
    // Mencoba melakukan koneksi dengan Database
    try {
      String url = "jdbc:odbc:Databases08";
      Properties prop = new Properties();
      konek = DriverManager.getConnection(url);
      status_Proses(true, "Sukses!!! Berhasil terhubung dengan Database ...",
                    20);
    }
    // Gagal Koneksi dengan Database
    catch (SQLException se) {
      status_Proses(false, "Gagal!!! Tidak Terhubung \nKarena : " + se, 20);
    }
  } // Akhir methode koneksiDatabase()

  public void check_User() {
    String user = "";
    String sql = "Select * from data_user";
    boolean user_valid = false;
    try {
      PreparedStatement stat = konek.prepareStatement(sql);
      ResultSet rset = stat.executeQuery();
      while (rset.next()) {
        user = rset.getString("user");
        if (user.equalsIgnoreCase(dataUser)) {
          user_valid = true;
          status_Proses(true, "Sukses!!! User Name Valid ...", 20);
        }
      }
      if (user_valid == false) {
        status_Proses(false, "Gagal!!! User yang Anda Masukkan Salah", 20);
      }
    }
    catch (SQLException se) {}
  }

  public void check_Password() {
    String password = "";
    String sql = "Select * from data_user where user = '" + dataUser + "'";
    boolean password_valid = false;
    try {
      PreparedStatement stat = konek.prepareStatement(sql);
      ResultSet rset = stat.executeQuery();
      while (rset.next()) {
        password = rset.getString("password");
        if (password.equalsIgnoreCase(dataPassword)) {
          password_valid = true;
          status_Proses(true, "Sukses!!! Password Valid ...", 20);
        }
      }
      if (password_valid == false) {
        status_Proses(false, "Gagal!!! Password yang Anda Masukkan Salah", 20);
      }
    }
    catch (SQLException se) {}
  }

  public void status_Proses(boolean sukses, String status, int nilaiProsesBar) {
    statusProses.setText(status);
    prosesBar.setValue(prosesBar.getValue() + nilaiProsesBar);
    if (proses == false) {
      JOptionPane.showMessageDialog(null,
                                    "Gagal Login ...\nError : " + statusGagal);
      removeAll();
      dispose();
      new LoginDialog_Database().main(null);
    }
    proses = sukses;
    statusGagal = status;
  }

  public void tahapan_prosesLogin() {
    task1 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        load_DriverJDBC();
        time.stop();
        if (proses == true) {
          time = new javax.swing.Timer(2000, task2);
          time.start();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }
      }
    };
    time = new javax.swing.Timer(1000, task1);
    time.start();

    task2 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        koneksiDatabase();
        time.stop();
        if (proses == true) {
          time = new javax.swing.Timer(2000, task3);
          time.start();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }

      }
    };

    task3 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        check_User();
        time.stop();
        if (proses == true) {
          time = new javax.swing.Timer(2000, task4);
          time.start();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }
      }
    };

    task4 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        check_Password();
        time.stop();
        if (proses == true) {
          time = new javax.swing.Timer(2000, task5);
          time.start();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }
      }
    };

    task5 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        status_Proses(true, "Proses Login Berhasil...", 20);
        time.stop();
        if (proses == true) {
          time = new javax.swing.Timer(2000, task6);
          time.start();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }
      }
    };
    task6 = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        if (prosesBar.getPercentComplete() == 1.0) {
          new utama_Perpustakaan();
          time.stop();
          dispose();
        }
        else {
          status_Proses(false, "ERROR", 20);
        }
      }
    };

  }
}
